﻿using UnityEngine;
using System.Collections;

public class KiritoAttack : StateMachineBehaviourEx 
{
    float slashCD = 1f;
    bool isSlashCD;

    float nextSlashTime = 0.2f;
    float nextSlashTimeStamp;

    Animator animator;

    public enum KiritoStates
    {
        Idle,
        LowerSlash,
        UpperSlash,
        JumpSlash
    }

    void Start()
    {
        animator = GetComponent<Animator>();
        currentState = KiritoStates.Idle;
    }

    #region Idle
    void Idle_Update()
    {
        if (Input.GetButtonDown("slash"))
        {
            currentState = KiritoStates.LowerSlash;

//             if (!isSlashCD)
//             {
//                 currentState = KiritoStates.Slash;
//                 isSlashCD = true;
//                 StartCoroutine(DoAfter(slashCD, ()=>isSlashCD = false));
//             }
        }
    }
    #endregion

    #region LowerSlash
    void LowerSlash_EnterState()
    {        
        animator.SetTrigger("lower slash");

        nextSlashTimeStamp = nextSlashTime + Time.time;

//         print("nextSlashTimeStamp : " + nextSlashTimeStamp);
//         print("Time.time : " + Time.time);

        //currentState = KiritoStates.UpperSlash;
    }

    void LowerSlash_Update()
    {
        if (animator.GetCurrentAnimatorStateInfo(1).IsName("lower slash"))
        {
            currentState = KiritoStates.UpperSlash;
        }
    }
    #endregion

    #region UpperSlash
    void UpperSlash_Update()
    {
//         if (Time.time > nextSlashTimeStamp)
//             currentState = KiritoStates.Idle;
// 
//         if (Input.GetButtonDown("slash") && Time.time > nextSlashTimeStamp - 0.3f)
//         {
//             animator.SetTrigger("upper slash");
//             currentState = KiritoStates.Idle;
//         }

        if (Time.time > nextSlashTimeStamp)
            currentState = KiritoStates.Idle;

        if (Input.GetButtonDown("slash"))
        {
            animator.SetTrigger("upper slash");
            currentState = KiritoStates.Idle;
        }
    }


    #endregion 

    #region JumpSlash
    void JumpSlash_EnterState()
    {
        animator.SetTrigger("lower slash");
    }
    #endregion


    IEnumerator Delay(float delay)
    {
        yield return new WaitForSeconds(delay);
    }

    IEnumerator DoAfter(float delay, System.Action operation)
    {
        yield return new WaitForSeconds(delay);
        operation();
    }
}
